package adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.sam.widget.FontTextView;
import button.StateImageButton;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import dialog.CustomDialog;
import entity.ContactEntityTest;
import entity.RecordEntity;
import tangyaxin.www.saoerweima.R;

/**
 * Created by Administrator on 2016/6/15.
 */
public class HistortRecordAdapter extends BaseAdapter {
    private Context context;
    private List<RecordEntity> recordEntityList;

    public HistortRecordAdapter(Context context, List<RecordEntity> recordEntityList) {
        this.context = context;
        this.recordEntityList = recordEntityList;
    }

    @Override
    public int getCount() {
        return recordEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return recordEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView==null){
            convertView=View.inflate(context,R.layout.item_activity_histort,null);
            viewHolder=new ViewHolder();
            viewHolder.recordType= (ImageView) convertView.findViewById(R.id.record_type);
            viewHolder.recordContent= (FontTextView) convertView.findViewById(R.id.recordContent);
            viewHolder.recordFrom= (FontTextView) convertView.findViewById(R.id.record_from);
            viewHolder.recordTime= (FontTextView) convertView.findViewById(R.id.recordTime);
            viewHolder.deleteBtn= (StateImageButton) convertView.findViewById(R.id.histort_delete);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        final RecordEntity entity= (RecordEntity) getItem(position);
        RecordEntity.RECORD_TYPE recordType=entity.getRecord_type();
        int[] recordTypeDrawable={R.mipmap.tmb_contact,R.mipmap.tmb_url,R.mipmap.tmb_text};
        viewHolder.recordType.setImageResource(recordTypeDrawable[recordType.ordinal()]);
        if (recordType== RecordEntity.RECORD_TYPE.CONTACT){
            ContactEntityTest contactEntityTest = (ContactEntityTest) entity.getRecordContent();
            viewHolder.recordContent.setText(contactEntityTest.getName());
        }else {
            viewHolder.recordContent.setText((String) entity.getRecordContent());
        }
        int[] recordFromResId={R.string.gray_icon,R.string.gray_scan};
        int[] recordFromColor={context.getResources().getColor(R.color.make_txt_color),context.getResources().getColor(R.color.scan_txt_color)};
        viewHolder.recordFrom.setText(recordFromResId[entity.getRecord_from().ordinal()]);
        viewHolder.recordFrom.setTextColor(recordFromColor[entity.getRecord_from().ordinal()]);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(context.getString(R.string.record_time_formater));
        String time=simpleDateFormat.format(new Date(entity.getRecordTime()));
        viewHolder.recordTime.setText(time);
        viewHolder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CustomDialog removeRecordDialog=new CustomDialog(context,R.layout.activity_histort_dialog);
                ((FontTextView)removeRecordDialog.findViewById(R.id.record_content)).setText(viewHolder.recordContent.getText());
                removeRecordDialog.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        recordEntityList.remove(position);
                        notifyDataSetChanged();
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.show();
            }
        });
        return convertView;
    }

    class ViewHolder{
        ImageView recordType;
        FontTextView recordContent,recordFrom,recordTime;
        StateImageButton deleteBtn;
    }
}
